package cn.edu.imnu.controller.shiro;

import cn.edu.imnu.mapper.WordUserMapper;
import cn.edu.imnu.model.WordUser;
import com.github.abel533.entity.Example;
import common.util.SysConstants;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * Shiro 自定义认证对象
 *
 * @author
 */

@Controller
public class CommonRealm extends AuthorizingRealm {
	private static Logger logger = LoggerFactory.getLogger(CommonRealm.class);


	@Resource
	private WordUserMapper userMapper;

	// public CommonRealm() {
	// 	setName(SysConstants.SHIRO_REALM_NAME);
	//
	// 	HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(
	// 			Sha256Hash.ALGORITHM_NAME);
	// 	matcher.setStoredCredentialsHexEncoded(false);
	// 	matcher.setHashIterations(1);
	// 	setCredentialsMatcher(matcher);
	// }

	/**
	 * 将字符串以 SHA-256 方式加密
	 *
	 * @param string
	 * @return
	 */
	// public static String encryptionBySha256Hash(String string) {
	// 	Sha256Hash sha256Hash = new Sha256Hash(string);
	// 	return sha256Hash.toBase64();
	// }

	/**
	 * 认证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		logger.info("用户登录认证开始.....");

		UsernamePasswordToken upToken = (UsernamePasswordToken) token;

		String username = upToken.getUsername();
		String password = String.copyValueOf(upToken.getPassword());
		Example example = new Example(WordUser.class);
		example.createCriteria().andEqualTo("username", username);
		// List<SysUser> userList = entityMapper.selectByExample(example);
		List<WordUser> userList = userMapper.selectByExample(example);
		WordUser user = null;
		if (userList.size() == 0) {
			throw new AuthenticationException("1");
		} else if (userList.size() != 1) {
			throw new AuthenticationException("2");
		} else if(!password.equals(userList.get(0).getPassword())){
			throw new AuthenticationException("3");
		}else {
			user = userList.get(0);
			// 向Session中添加用户对象
			SecurityUtils.getSubject().getSession()
					.setAttribute(SysConstants.SESSION_USER_KEY, user);
			//设置session 永不过期
			SecurityUtils.getSubject().getSession().setTimeout(-1001);
		}

		return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), getName());
	}

	/**
	 * 授权
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(
			PrincipalCollection collection) {
		logger.info("用户登录授权开始.....");

		// String userName = (String)
		// collection.fromRealm(getName()).iterator().next();

		WordUser user = (WordUser) SecurityUtils.getSubject().getSession()
				.getAttribute(SysConstants.SESSION_USER_KEY);
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		if (user == null) {
			logger.info("用户未登录！");
		} else {
			Set<String> rolePermission = new HashSet<String>();
			Set<String> purviewPermission = new HashSet<String>();

			info.addRoles(rolePermission);

			info.addStringPermissions(purviewPermission);
		}
		logger.info("current user is " + user);

		return info;
	}


}
